// 角色管理模块的接口
import request from '@/utils/request'
import { RoleResponseData, RoleData, MenuResponseData } from './type'

// 枚举地址
enum API {
  // 获取全部的职业接口
  ALLROLE_URL = '/admin/acl/role/',
  // 新增职位的接口
  ADDROLE_URL = '/admin/acl/role/save',
  // 更新已有职位的接口
  UPDATEROLE_URL = '/admin/acl/role/update',
  // 获取全部的菜单与按钮的数据
  ALLPERMISSION_URL = '/admin/acl/permission/toAssign/',
  // 分配职位权限的接口
  SETPERMISTION_URL = '/admin/acl/permission/doAssign/',
}

// 获取全部的角色
export const reqAllRoleList = (page: number, limit: number, roleName: string) =>
  request.get<any, RoleResponseData>(API.ALLROLE_URL + `${page}/${limit}`, {
    params: { roleName },
  })

// 获取全部的角色
export const reqAddOrUpdateRole = (data: RoleData) => {
  if (data.id) {
    return request.put(API.UPDATEROLE_URL, data)
  } else {
    return request.post(API.ADDROLE_URL, data)
  }
}

// 获取全部菜单与按钮权限数据
export const reqAllMenuList = (roleId: number) =>
  request.get<any, MenuResponseData>(API.ALLPERMISSION_URL + roleId)

// 给相应的职位下发权限
export const reqSetPermisstion = (roleId: number, permissionId: number[]) =>
  request.post<any, any>(API.SETPERMISTION_URL, null, {
    params: {
      roleId,
      permissionId: permissionId.join(','), // 将数组转换为逗号分隔的字符串
    },
  })
